﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ClassLibrary.Entity;
using ClassLibrary.DAO;

public partial class UserControl_ucActionList : System.Web.UI.UserControl
{
    TblJobNoteDAO jobDAO = new TblJobNoteDAO(Global.CONNECTION_STRING);
    TblStatusJobDAO StatusJobDAO = new TblStatusJobDAO(Global.CONNECTION_STRING);

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            rd1.Attributes.Add("click", "GetValueNoteType()");
            rd2.Attributes.Add("click", "GetValueNoteType()");
            rd3.Attributes.Add("click", "GetValueNoteType()");
            rd4.Attributes.Add("click", "GetValueNoteType()");
            rd5.Attributes.Add("click", "GetValueNoteType()");
            //loadJobNotes();
        }

    }

    private void loadJobNotes()
    {
        try
        {
            int id = Global.ConvertToInt(Request.QueryString["id"]);
            grdJobNote.DataSource = jobDAO.GetJobNotes(id);
            grdJobNote.DataBind();
        }
        catch (Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
            Response.Redirect(Global.ERROR_PAGE);
        }

    }

    protected void bt_save_Click(object sender, EventArgs e)
    {
        int id = Global.ConvertToInt(Request.QueryString["id"]);
        int id1 = Global.ConvertToInt(Request.QueryString["idJobNote"]);

        bool flag = false;
        try
        {

            TblJobNotes jobnote = new TblJobNotes();
            jobnote.JobID = id;
            jobnote.Notes = txtNotes.Text + " .ACTION BY " + txt_ActionBy.Text.Trim();

            jobnote.DateEntered = DateTime.Now;

           
            //jobnote.StatusJob = 0;

            if (rd3.Checked == true)
            {
                jobnote.StatusJobNote = "NAR";
            }
            else if (rd4.Checked == true)
            {
                jobnote.StatusJobNote = "To Be Actioned";
                jobnote.StatusJob = 0;

            }
            else if (rd5.Checked == true)
            {
                jobnote.StatusJobNote = "Has been Actioned";
                jobnote.StatusJob = 1;
                jobDAO.Update_JobStaus(Convert.ToInt32(id));
            }

            if (rd1.Checked == true)
            {
                jobnote.NoteType = "Requires Action";
                jobDAO.InsUp_JobNote(jobnote, 1);

            }
            else if (rd2.Checked == true)
            {
                jobnote.NoteType = "No Action Requires";

                #region StatusJob

                TblJobStatus JobStatus = new TblJobStatus();
                JobStatus.JobID = Convert.ToInt32(id);
                JobStatus.StatusDate = Convert.ToDateTime(DateTime.Now);
                JobStatus.StatusJobs = "New Update";
                JobStatus.Notes = txtNotes.Text;
                StatusJobDAO.JobStatusIns(JobStatus);

                #endregion
            }

            //TblJobNotes jobNote1 = new TblJobNotes();
            //jobNote1.JobID = id;
            //jobNote1.Notes = txtNotes.Text;
            //jobNote1.NoteType = txt_NoteType.Text;
            //jobNote1.DateEntered = DateTime.Now;
            //jobNote1.StatusJobNote = txt_status.Text;
            //jobNote1.IDJobNote = id1;

            //jobDAO.InsUp_JobNote(jobNote1, 2);

            //DataTable dtStatus = StatusJobDAO.LoadLastStatus(id);
            //string idStatus = "";
            //foreach (DataRow dr in dtStatus.Rows)
            //{
            //    idStatus = dr["ID"].ToString();
            //}

            //StatusJobDAO.UpdateNote(id, txtNotes.Text,Convert.ToInt32(idStatus));

            flag = true;


        }
        catch (Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
            Response.Redirect(Global.ERROR_PAGE);
        }
        if (flag)
        {
            loadJobNotes();
            //lblResult.Text = "successful";
            // message_green.Style.Value = "inline";
            //Response.Redirect("Default.aspx");
            //loadJobNotes();
            ScriptManager.RegisterStartupScript(bt_save, bt_save.GetType(), "ScriptLoginSuccess", "UpdateParentsLogin();", true);
            //Response.Redirect("~/Booking.aspx");
        }
    }
}
